const Service = require('../../../core/service/ApiService');

class StaffSvc extends Service {

    //搜索服务人员
    async searchStaff(pn, ps, kw) {
        const dao = this.service.admin.staff.dao;
        const data = await dao.searchStaff(pn, ps, kw);
        return {
            list: data.rows,
            count: data.count
        }
    }

    // 获取服务人员详细信息
    async getStaffDetail(id) {
        const dao = this.service.admin.staff.dao;
        const detailFromDb = await dao.getStaffDetail(id);
        if (!detailFromDb)
            throw new this.error.BusinessError('不存在该服务人员');
        const birth = detailFromDb.birth.toString();
        const strBirthday = birth.substr(0, 4) + '/' + birth.substr(4, 2) + '/' + birth.substr(6, 2);
        const birthDate = new Date(strBirthday);
        const nowDateTime = new Date();
        let age = nowDateTime.getFullYear() - birthDate.getFullYear();
        // 再考虑月、天的因素;.getMonth()获取的是从0开始的，这里进行比较，不需要加1
        if (nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() === birthDate.getMonth() && nowDateTime.getDate() < birthDate.getDate())) {
            age--;
        }
        const arr_cmu_id = detailFromDb.community_id_list.split(',');
        return {
            svc_staff_id: detailFromDb.svc_staff_id,
            pic: detailFromDb.pic,
            name: detailFromDb.name,
            phone: detailFromDb.phone,
            age: age,
            id_card_num: detailFromDb.id_card_num,
            address: detailFromDb.address,
            community_list: await dao.getCommunityName(arr_cmu_id)
        };

    }

    //管理员重置密码
    async resetPwd(id) {
        const dao = this.service.admin.staff.dao;
        const staffInfo = await dao.findStaffInfoById(id);
        if (!staffInfo) {
            throw new this.error.BusinessError('不存在该服务人员')
        }
        const encryptedPwd = this.helper.sha1(staffInfo.phone.substring(5));
        await dao.resetPwd(id, encryptedPwd);
    }

    //编辑服务人员信息
    async editStaffDetail(info) {
        const dao = this.service.admin.staff.dao;
        info.community_id_list = info.community_list.join(',');
        await dao.editStaffDetail(info);
    }

    //添加服务人员
    async addSvcStaff(info) {
        const pwd = info.phone.substring(5);
        const encryptedPwd = this.helper.sha1(pwd);
        const birth = info.id_card_num.substr(6, 8);
        const newInfo = {
            name: info.name,
            sex: info.sex,
            pic: info.pic,
            birth: birth,
            address: info.address,
            phone: info.phone,
            pwd: encryptedPwd,
            id_card_num: info.id_card_num,
            community_id_list: info.community_list.join(','),
            town_id: this.ctx.session.town_id
        };
        await this.service.admin.staff.dao.addSvcStaff(newInfo);
    }

    //批量添加服务人员
    async importStaff(arrInfo) {
        arrInfo.forEach(item => {
            item.pwd = this.helper.sha1(item.phone.substring(5));
            item.town_id = this.ctx.session.town_id;
            item.community_id_list = item.community_list.join(',')

        });
        await this.service.admin.staff.dao.importStaff(arrInfo);
    }

    //删除服务人员信息
    async deleteStaff(List) {
        await this.service.admin.staff.dao.deleteStaff(List);
    }

    //修改服务人员状态
    async enableStaff(svc_staff_id, status) {
        await this.service.admin.staff.dao.enableStaff(svc_staff_id, status);
    }
}


module.exports = StaffSvc;
